package com.huage_08.lesson_02;

public class SellTicket implements Runnable{
    private int ticket = 20;
    private int flag = 1;
    @Override
    public void run() {
        while (flag>0){
//            synchronized (object){
//                if (ticket>0){
//                    try {
//                        Thread.sleep(100);//出票时间
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    System.out.println(Thread.currentThread().getName()+"卖出了第"+ticket+"号票");
//                    ticket--;

                       sellTicket();

//            }
        }
    }
    public synchronized void sellTicket() {
            if (ticket > 0) {
                try {
                    Thread.sleep(100);//出票时间
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket + "号票");
                ticket--;
            } else {
                System.out.println("票已卖完!");
                flag=0;
            }
        }
}
